package com.itheima.mybatis_plus.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mybatis_plus.pojo.Course;
import com.itheima.mybatis_plus.pojo.User;
import com.itheima.mybatis_plus.service.CourseService;
import com.itheima.mybatis_plus.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author huyoufu <https://github.com/huyoufu>
 * @TIME 2021/10/9 8:45
 * @description
 */
@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;
    @GetMapping("/findAll")
    public List findAll(){
       List<Course> list= courseService.findAll();

       return list;
    }
    @GetMapping("/findById")
    public Course findById(@RequestParam("id") int id){
        return  courseService.findById(id);
    }
    @PostMapping("/add")
    public String add(@RequestBody Course course){
        courseService.add(course);
        return "success";
    }
    @PutMapping("/update")
    public String update(@RequestBody Course course){
        courseService.update(course);
        return "success";
    }
    @DeleteMapping("/delete")
    public String delete(@RequestParam("id") int id){
        courseService.deleteById(id);
        return "success";
    }

    @GetMapping("/page1")
    public Page page1(int pageNumber, int pageSize){
        return courseService.page1(pageNumber,pageSize);
    }

    @GetMapping("/findByCondition")
    public List findByCondition(String name,@RequestParam(required = false) Integer userId){
        //return courseService.findByCondition(name,userId);
        LambdaQueryWrapper<Course> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.eq(Course::getName,name);
        courseService.list(queryWrapper);
        return null;
    }





}
